package com.wmh.baseservice.robot.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.admin.service.AdRoleService;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.robot.entity.RobotTaskStatement;
import com.wmh.baseservice.robot.mapper.RobotTaskStatementMapper;
import com.wmh.baseservice.robot.pojo.dto.TaskStatementAddDTO;
import com.wmh.baseservice.robot.pojo.dto.TaskStatementFileReps;
import com.wmh.baseservice.robot.pojo.vo.TaskStatementAppendResultReqVO;
import com.wmh.baseservice.robot.service.RobotTaskStatementService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Base64;
import java.util.Objects;

/**
 * @author 谢骏
 * @description 针对表【robot_task_statement(机器人任务报表)】的数据库操作Service实现
 * @createDate 2023-03-16 10:03:40
 */
@Service
@Valid
public class RobotTaskStatementServiceImpl extends ServiceImpl<RobotTaskStatementMapper, RobotTaskStatement>
        implements RobotTaskStatementService {

    @Resource
    private HttpServletRequest request;

    @Resource
    private AdRoleService adRoleService;

    @Resource
    private UserUtil userUtil;

    @Override
    public boolean add(@Validated TaskStatementAddDTO taskStatementAddDTO) {
        RobotTaskStatement taskStatement = new RobotTaskStatement().setId(taskStatementAddDTO.getStatementId());
        BeanUtils.copyProperties(taskStatementAddDTO, taskStatement);
        return save(taskStatement);
    }

    @Override
    public void appendResult(@Validated TaskStatementAppendResultReqVO reqVO) {
        RobotTaskStatement statement = new RobotTaskStatement();
        BeanUtils.copyProperties(reqVO, statement);
        statement.setId(reqVO.getStatementId());
        if (!updateById(statement)) {
            throw new ServiceException("追加任务报表结果失败");
        }
    }

    @Override
    public TaskStatementFileReps getTaskStatementFile(Long statementFileId) {
        RobotTaskStatement statement = checkTaskStatementExist(statementFileId);
        checkDwTaskStatementFilePermission(statement);
        if (StrUtil.isBlank(statement.getTaskFileBase64()) || StrUtil.isBlank(statement.getTaskFileSuffix())) {
            throw new ServiceException("该报表不存在附件或附件扩展名 : " + statementFileId);
        }
        return new TaskStatementFileReps()
                .setContent(Base64.getDecoder().decode(statement.getTaskFileBase64()))
                .setFileFullName(statement.getTaskName() + "." + statement.getTaskFileSuffix());
    }

    @Override
    public TaskStatementFileReps getTaskStatementLog(Long statementFileId) {
        RobotTaskStatement statement = checkTaskStatementExist(statementFileId);
        checkDwTaskStatementFilePermission(statement);
        if(StrUtil.isBlank(statement.getTaskLogBase64())){
            throw new ServiceException("该报表不存在日志文件");
        }
        return new TaskStatementFileReps().setContent(Base64.getDecoder().decode(statement.getTaskLogBase64()))
                .setFileFullName(statement.getTaskName() + ".log");
    }

    private RobotTaskStatement checkTaskStatementExist(Long statementFileId){
        RobotTaskStatement statement = getOne(new QueryWrapper<RobotTaskStatement>().eq(RobotTaskStatement.COL_ID, statementFileId));
        if (Objects.isNull(statement)) {
            throw new ServiceException("该编号的报表已不存在 : " + statementFileId);
        }
        return statement;
    }

    private void checkDwTaskStatementFilePermission(RobotTaskStatement statement){
        Long adId = userUtil.getId();
        boolean hasSupperAdmin = adRoleService.isHasSupperAdmin(adId);
        // 非超级管理员 不能下载他人的模板报表
        if (!hasSupperAdmin && Objects.nonNull(statement.getAdId()) && !Objects.equals(adId, statement.getAdId())) {
            throw new ServiceException("您没有权限下载他人报表的文件内容 : " + statement.getId());
        }
    }
}




